/**
 * 		-	ad-creatif	-
 * 		-	20/07/2012	-
 *
 * Critical class storing all params of the game. 
 * The player is created there.
 * There's also some tools lik loading, saving, pausing, cleaning ram
 * wich will be moved soon
 * 
 **/
package {
	import blocks.Checkpoint;
	
	import entities.Enemy;
	import entities.Player;
	
	//import flash.errors.MemoryError;
	//import flash.utils.getDefinitionByName;
	
	import org.flixel.*;
	
	import screens.FlxPaused;
	import screens.HudScreen;
	
	import tools.AssetsLoader;
	import tools.Fx;
	import tools.SaveManager;
	
	public class Registry {
		
		[Embed(source="../data/cursor.png")] public static var cursorPNG:Class;
		[Embed(source="../data/blank.png")] public static var blankPNG:Class;
		[Embed(source="../data/blankTiles.png")] public static var blankTilesPNG:Class;
		[Embed(source="../data/blankLevel.txt", mimeType = "application/octet-stream")] public static var blankLevel:Class;
		
		
		//miscelancious
		public static var pauseScreen:FlxPaused;
		public static var save:SaveManager;
		
		
		//blocs
		//public static var doorPool:FlxGroup; 		// contains DOORS
		public static var solidTilesPool:FlxGroup;	// contains COLLIDABLE BLOCS
		public static var dynamicPool:FlxGroup;		// contains NON COLLIDABLE BLOCKS
		
		public static var entityPool:FlxGroup;		// contains ALL entities (bullets,player,ennemies...)
		
		public static var hostilePool:FlxGroup;		// contains HOSTILES stuffs
		public static var friendlyPool:FlxGroup;	// contains FRIENDLY stuffs
		
		public static var enemyPool:FlxGroup;		// contains ENEMY
		public static var enemyBulletPool:FlxGroup; // contains ENNEMY BULLETS
		
		public static var bulletPool:FlxGroup;		// contains FRIENDLY BULLETS
		public static var player:Player;			// the player
		
		public static var level:Level;				// contains MAIN LEVEL
		public static var hud:HudScreen;			// HUD
		public static var fx:Fx;					// FX
		
		//params imported from XML
		public static var gameList:Object;
		public static var gameName:String;
		public static var gameInfos:Object;
		
		public static var tileInfos:Object;
		public static var blockInfos:Object;
		public static var weaponInfos:Object;
		public static var entityInfos:Object;
		
		public static var playerNames:Array;		// used mainly for switching player entity
		public static var abilities:Object;			// define what attack entity can do depending of their level
		public static var attacks:Object;			// the attacks properties like duration, sounds, assets...
		
		public static var loaded:Boolean;
		public static var paused:Boolean;
		
		public function Registry() { }
		
		public static function init(onReadyCallback:Function):void {
			AssetsLoader.massLoader(function():void {
				FlxG.score = 0;

				entityPool = new FlxGroup();
				friendlyPool = new FlxGroup();
				hostilePool = new FlxGroup();
				enemyPool = new FlxGroup();
			
				level = new Level;
				
				playerNames = []
				// sort names for switching and select the first player by name
				for each (var infos:Object in Registry.entityInfos) 
				if(infos.type == 'player') playerNames.push(infos.name);
				
				playerNames.sort();
				
				if(!gameInfos.world.startPos) gameInfos.world.startPos = [2,2];
				
				if(playerNames.length < 1 || !entityInfos[playerNames[0]].name) {
					trace('ALERTE : player n\'est pas défini')
					return;
				}
				
				player = new Player(
					gameInfos.world.startPos[0] * level.tileSize[0], 
					gameInfos.world.startPos[1] * level.tileSize[1], 
					entityInfos[playerNames[0]]
				);
				player.score = (player.score) ? player.score : 0;
				player.level = (gameList[gameName].level) ? gameList[gameName].level : 0;
				player.infos.gamePlay.lives = (gameList[gameName].lives) ? gameList[gameName].lives : player.infos.gamePlay.lives;
				
				fx = new Fx;
				
				hud = new HudScreen;
				
				save = new SaveManager;
				
				onReadyCallback();
				
				loaded = true;
				paused = false;
			});
		}
		
		
		public static function erase():void {
			entityPool = null;
			solidTilesPool = null;
			dynamicPool = null;
		}
		
	
		public static function pause(level:FlxState):void{
			pauseScreen = new FlxPaused(level);			
			pauseScreen.showPaused();
			level.setAll('active', false, true);
			hud.add(Registry.pauseScreen);
		}
		
		
		public static function unPause(level:FlxState):void{
			pauseScreen.hidePaused();
			pauseScreen = null;
		}
	}
}
